#include "include.h"

void listbox_message(u16 msg)
{
    switch (msg) {
        case KU_PLAY:
        case KU_PLAY_POWER:
        case KU_PLAY_MODE:
            listbox_cb.sta = LB_STA_OK_KEY;
            break;

        case KL_PLAY:
            listbox_cb.sta = LB_STA_EXIT;
            break;

        case KL_REC:
            listbox_cb.sta = LB_STA_DEL_KEY;
            break;

        case KU_PREV_VOL_DOWN:
        case KL_VOL_DOWN_PREV:
        case KU_PREV:
            listbox_prev_item();
            break;

        case KU_NEXT_VOL_UP:
        case KL_VOL_UP_NEXT:
        case KU_NEXT:
            listbox_next_item();
            break;

        case EVT_UDISK_REMOVE:
            listbox_cb.evt_dev = DEV_UDISK;
            listbox_cb.sta = LB_STA_DEV_OUT;
            break;

        case EVT_SD_REMOVE:
            listbox_cb.evt_dev = DEV_SDCARD;
            listbox_cb.sta = LB_STA_DEV_OUT;
            break;

        case EVT_SD1_REMOVE:
            listbox_cb.evt_dev = DEV_SDCARD1;
            listbox_cb.sta = LB_STA_DEV_OUT;
            break;

        case EVT_UDISK_INSERT:
            listbox_cb.evt_dev = DEV_UDISK;
            listbox_cb.sta = LB_STA_DEV_IN;
            break;

        case EVT_SD1_INSERT:
            listbox_cb.evt_dev = DEV_UDISK;
            listbox_cb.sta = LB_STA_DEV_IN;
            break;

        case EVT_SD_INSERT:
            listbox_cb.evt_dev = DEV_SDCARD1;
            listbox_cb.sta = LB_STA_DEV_IN;
            break;

        default:
            func_message(msg);
            break;
    }
}

void listbox_draw_line(char *str, u8 y)
{
    u8 item_flag;
    u32 icon_index;
    u8 x = 18;

    item_flag = str[listbox_cb.item_len - 1];
    if ((item_flag & FB_GBK) == 0) {    //unicode
        utf16_convert_to_gbk(str, str[98]);
        #if CHARACTER_ENC
        #else
        str[listbox_cb.item_len - 1] |= FB_GBK;
        #endif // CHARACTER_ENC
    }
    #if CHARACTER_ENC
    else{
        f_msc.unicode_r = 0;
    }
    #endif // CHARACTER_ENC
    switch (item_flag & FB_ICON_MASK) {
        case FB_FOLDERUP:
            icon_index = BMP_SYS_ICON_FOLDERUP;
            break;
        case FB_FOLDER:
            icon_index = BMP_SYS_ICON_FOLDER;
            break;
        case FB_FILE:
            icon_index = BMP_SYS_ICON_FILE;
            break;
        case FB_MUSIC:
            icon_index = BMP_SYS_ICON_SING;
            break;
        default:
            icon_index = BMP_SYS_ICON_ITEM;
            break;
    }
    lcd_line_clear(y);
    lcd_draw_text(str, (s16)x + nmv_cb.x, y, 1);
    if (listbox_cb.icon_en) {
        lcd_draw_image(RES_BUF_BMP_SYS_DAT, icon_index, 0, y);  //显示图标
    }
    lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_CENTER_LINE, 124, y);
}

u8 bmp_sildebar_tbl[4] = {0};

void prepare_slidebar(void)
{
    listbox_cb_t *s = &listbox_cb;

    u8 h, c, idx, min, max;
    h = LCD_HEIGHT / s->total;
    if (h < 2) {
        h = 2;
    } else if (h > 16) {
        h = 16;
    }
    c = 16 - h;
    idx = c == 0 ? 0 : s->page_index * c / (s->total - s->page_nitem);
    for (int i=0; i<4; i++) {
        if (idx > i * 4 + 3 || idx + h - 1 < i * 4) {
            bmp_sildebar_tbl[i] = 0 << 4;
        } else if (idx <= i * 4 && idx + h - 1 >= i * 4 + 3) {
            bmp_sildebar_tbl[i] = 0 | 4 << 4;
        } else {
            min = idx < i * 4 ? i * 4 : idx;
            max = idx + h - 1 > i * 4 + 3 ? i * 4 + 3 : idx + h - 1;
            bmp_sildebar_tbl[i] = min % 4 | (max - min + 1) << 4;
        }
    }
}

void lcd_draw_slidebar(u8 line) {
    u8 l = line/2;
    if ((bmp_sildebar_tbl[l] >> 4) > 0) {
        if ((bmp_sildebar_tbl[l] & 0x0f) == 0) {
            if ((bmp_sildebar_tbl[l] >> 4) == 1) {
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB01, 125, line);
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB01, 127, line);
            } else if ((bmp_sildebar_tbl[l] >> 4) == 2) {
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB02, 125, line);
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB02, 127, line);
            } else if ((bmp_sildebar_tbl[l] >> 4) == 3) {
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB31, 125, line);
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB31, 127, line);
                lcd_disp_invert(125, line, 1, 2);
                lcd_disp_invert(127, line, 1, 2);
            } else if ((bmp_sildebar_tbl[l] >> 4) == 4) {
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB04, 125, line);
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB04, 127, line);
            }
        }
        if ((bmp_sildebar_tbl[l] & 0x0f) == 1) {
            if ((bmp_sildebar_tbl[l] >> 4) == 2) {
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB12, 125, line);
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB12, 127, line);
            } else if ((bmp_sildebar_tbl[l] >> 4) == 3) {
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB01, 125, line);
                lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB01, 127, line);
                lcd_disp_invert(125, line, 1, 2);
                lcd_disp_invert(127, line, 1, 2);
            }
        }
        if ((bmp_sildebar_tbl[l] & 0x0f) == 2 && (bmp_sildebar_tbl[l] >> 4) == 2) {
            lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB02, 125, line);
            lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB02, 127, line);
            lcd_disp_invert(125, line, 1, 2);
            lcd_disp_invert(127, line, 1, 2);
        }
        if ((bmp_sildebar_tbl[l] & 0x0f) == 3 && (bmp_sildebar_tbl[l] >> 4) == 1) {
            lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB31, 125, line);
            lcd_draw_image(RES_BUF_BMP_SLIDEBAR_DAT, BMP_SLIDEBAR_SUB31, 127, line);
        }
    }
}


///列表框显示
void listbox_display(void)
{
    u8 pos, line;
    u8 num;
    listbox_cb_t *s = &listbox_cb;
    char *view = s->view;

    if (box_cb.disp_sta != GUI_BOX_NULL) {
        nmv_cb.en = 0;
        return;
    }

    if (s->cindex == s->disp_index) {
        if ((nmv_cb.en) && (nmv_cb.ms_cnt)){
            nmv_cb.ms_cnt = 0;
            if ((nmv_cb.x + (s16)nmv_cb.wide) > 100) {
                if (nmv_cb.x == 0) {
                    nmv_cb.hold_cnt++;
                    if (nmv_cb.hold_cnt >= 10) {    //文件头Hold显示1秒
                        nmv_cb.hold_cnt = 0;
                        nmv_cb.x--;
                    }
                } else {
                    nmv_cb.x--;
                }
            } else {
                nmv_cb.hold_cnt++;
                if (nmv_cb.hold_cnt >= 10) {        //文件尾Hold显示1秒
                    nmv_cb.hold_cnt = 0;
                    nmv_cb.x = 0;
                }
            }
            pos = s->cindex - s->page_index;
            view += s->item_len * pos;
            line = pos * 2;
            lcd_line_clear(line);
            listbox_draw_line(view, line);
            prepare_slidebar();
            lcd_draw_slidebar(line);
            lcd_disp_invert(0, line, 124, 2);
            lcd_line_update(line, 2);
        }
        return;
    }

    lcd_buf_clear();
    s->disp_index = s->cindex;
    num = s->total - s->page_index;
    if ((s->total - s->page_index) > s->page_nitem) {
        num = s->page_nitem;
    }
    pos = s->cindex - s->page_index;
    line = 0;
    prepare_slidebar();
    while (num--) {
        nmv_cb.x = 0;
        listbox_draw_line(view, line);
        lcd_draw_slidebar(line);
        if (pos-- == 0) {
            nmv_cb.hold_cnt = 0;
            nmv_cb.wide = get_name_str_width(view);
            if (nmv_cb.wide > 108) {
                nmv_cb.en = 1;
            } else {
                nmv_cb.en = 0;
            }
            lcd_disp_invert(0, line, 124, 2);
        }
        lcd_line_update(line, 2);
        view += s->item_len;
        line += 2;
    }
    while (line < 8) {
        lcd_line_update(line, 2);
        line += 2;
    }
}
